package top.yuki.setting;


import lombok.SneakyThrows;
import top.yuki.LevelWorld;
import lombok.Getter;

import java.io.*;

@Getter
public class FileManager {
    @Getter
    private static FileManager instance;
    public static FileManager init(LevelWorld lw) {
        return instance = new FileManager(lw);
    }          		//简单生成对象的方法


    private LevelWorld lw;
    public File dataFolder;
    public File configFile;
    public File dataFile;


    public FileManager(LevelWorld lw){
        this.lw = lw;
    }
    public boolean generate(){
        dataFolder = lw.getDataFolder();
        configFile = new File(dataFolder, "config.yml");
        if (!configFile.exists()) {

            lw.saveDefaultConfig();
        }
        dataFile = new File(dataFolder, "data.yml");
        if (!dataFile.exists()) {
            try {
                dataFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



        return true;
    }


    public boolean saveFile(String name, File target)  {
        InputStream in = lw.getResource(name);
        try {
            OutputStream out = new FileOutputStream(target);
            byte[] buffer = new byte[10240]; // 10KB
            int len = -1;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
    }